import { IProtocol } from "../IProtocol";
import { ProtocolHead } from "./ProtocolHead";
import { IBusinessHandler } from "../../Util/IBusinessHandler";
import { IIOHandler } from "../../Util/IIOHandler";
import ByteArray = require("bytearray-node");
type int = number;
//class Protocol

export class Protocol implements IProtocol {
  constructor() {
    this._bytesBuffer.endian = "LE";
  }
  public handleData(
    bytes_data: ByteArray,
    business_handler1: IBusinessHandler,
    io_handler: IIOHandler,
    business_handler2: IBusinessHandler
  ): int {
    // 将bytes_data放入缓冲区后面
    this._bytesBuffer.position = this._bytesBuffer.length;
    this._bytesBuffer.writeBytes(bytes_data, 0, bytes_data.length);
    this._bytesBuffer.position = 0;
    for (;;) {
      if (this._readHead) {
        // 已经读过head，开始处理body
        if (
          this._bytesBuffer.length - this._bytesBuffer.position >=
          this._protocolHead.messageSize
        ) {
          // 处理一块数据
          var bytes_body = new ByteArray();
          bytes_body.endian = "LE";
          // bytes_body.endian = flash.utils.Endian.LITTLE_ENDIAN;
          this._bytesBuffer.readBytes(
            bytes_body,
            0,
            this._protocolHead.messageSize
          );
          this._readHead = false;
          if (this._protocolHead.compressionStatus == 1) {
            bytes_body.uncompress();
          }
          if (!business_handler1.handlePacket(bytes_body, business_handler2)) {
            return -1;
          }
        } else {
          break;
        }
        continue;
      }
      if (
        this._bytesBuffer.length - this._bytesBuffer.position <
        ProtocolHead.CDF_PROTOCOL_HEAD_SIZE
      ) {
        break;
      }
      // 读取head
      this._protocolHead.__read(this._bytesBuffer);
      this._readHead = true;
      if (
        !(
          this._protocolHead.messageSize > ProtocolHead.CDF_PROTOCOL_MAX_SIZE ||
          this._protocolHead.messageSize < 0
        )
      ) {
        continue;
      }
      return -1;
    }
    if (this._bytesBuffer.position != 0) {
      // 还有数据余留，保留剩下缓冲区
      var bytes_remain = new ByteArray();
      bytes_remain.writeBytes(this._bytesBuffer, this._bytesBuffer.position);
      this._bytesBuffer = bytes_remain;
      this._bytesBuffer.endian = "LE";
      // this._bytesBuffer.endian = flash.utils.Endian.LITTLE_ENDIAN;
    }
    return bytes_data.length;
  }

  /**
   * 发送数据
   * @param bytes_data
   * @param io_handler
   * @return
   */
  public sendDataEncrypt(
    bytes_data: ByteArray,
    io_handler: IIOHandler
  ): Boolean {
    if (bytes_data.length > ProtocolHead.CDF_PROTOCOL_MAX_SIZE) {
      return false;
    }
    var bytes = new ByteArray();
    bytes.endian = "LE";
    // bytes.endian = flash.utils.Endian.LITTLE_ENDIAN;
    var head = new ProtocolHead();
    head.messageSize = bytes_data.length;
    // 写入头
    head.__write(bytes);
    bytes.writeBytes(bytes_data, 0, bytes_data.length);
    return !(io_handler.sendData(bytes) == -1);
  }

  public clear(): void {
    this._bytesBuffer.clear();
    this._readHead = false;
  }

  private _bytesBuffer: ByteArray = new ByteArray();

  private _readHead: boolean = false;

  private _protocolHead: ProtocolHead = new ProtocolHead();
}
